package com.project.mall.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.project.mall.mapper.MapperH;
import com.project.mall.pojo.*;
import com.project.mall.service.ServiceH;
import com.project.mall.service.ServiceW;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 黄键松
 */
@Controller
public class ControllerH {
    @Resource
    private ServiceH serviceH;
    @Resource
    private ServiceW serviceW;
    @RequestMapping("/index")
    public String getInd( Model model){
        //查询所有一级二级分类
        List<Secondtype> firAndSec = serviceH.getAllFirAndSec();
        model.addAttribute("firAndSec",firAndSec);
        //获取明星单品
        List<Goods> hotGoods = serviceH.getHotGoods();
        model.addAttribute("hot",hotGoods);
        //根据一级id获取商品列表
        List<Goods> firlsit = serviceH.getGoodListByFir(1);
        model.addAttribute("firlist",firlsit);
        List<Goods> seclsit = serviceH.getGoodListByFir(2);
        model.addAttribute("seclsit",seclsit);
        List<Goods> thirlsit = serviceH.getGoodListByFir(3);
        model.addAttribute("thirlsit",thirlsit);
        List<Goods> fourlsit = serviceH.getGoodListByFir(4);
        model.addAttribute("fourlsit",fourlsit);
        List<Goods> fivlsit = serviceH.getGoodListByFir(5);
        model.addAttribute("fivlsit",fivlsit);
        List<Goods> sixlsit = serviceH.getGoodListByFir(6);
        model.addAttribute("sixlsit",sixlsit);
        //广告位
        Goods goods1 = serviceW.getGoods(10101);
        model.addAttribute("goods1",goods1);
        Goods goods2 = serviceW.getGoods(40202);
        model.addAttribute("goods2",goods2);
        Goods goods3 = serviceW.getGoods(5050202);
        model.addAttribute("goods3",goods3);
        return "before/index";
    }


    /**
     * 点击一级分类或者更多时候加载
     * 根据一级id查询所有商品信息
     * @return
     */
    @RequestMapping("/list/{id}")
    public String getGoodsList(@PathVariable Integer id,Model model){
        if(id==null||id==0){
            return "after/404";
        }else{
            List<Goods> firgoods = serviceH.getGoodsListByFirid(id);
            model.addAttribute("firgoods",firgoods);
            return "before/goods-list";
        }
    }
    /**
     * 点击一级分类或者更多时候加载
     * 根据一级id查询所有商品信息
     * @return
     */
    @RequestMapping("/list02/{id}/{secname}")
    public String getGoodsList2(@PathVariable Integer id,@PathVariable String secname, Model model){
        if(id==null||id==0){
            return "after/404";
        }else{
            List<Goods> secgoods = serviceH.getGoodsListBySecname(id,secname);
            model.addAttribute("firgoods",secgoods);
            return "before/goods-list";
        }
    }

    /**
     * 查询搜索功能
     * @param gname
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String getGoodsList3(@RequestParam String gname, Model model){
        List<Goods> search = serviceH.searchGoods(gname);
        if(search.size()==0){
            return "before/goods-lost";
        }
        model.addAttribute("firgoods",search);
        return "before/goods-list";
    }
    //**********************************************购物车板块*************************************

    /**
     * 点击添加购物车
     * @param gid
     * @param gtyprid
     * @param gcolorid
     * @param count
     * @param
     * @return
     */
    @RequestMapping("/shoppingcar/{gid}/{gtypeid}/{gcolorid}/{count}")
    @ResponseBody
    public String shppingcar(@PathVariable("gid")Integer gid,@PathVariable("gtypeid")Integer gtyprid,
                             @PathVariable("gcolorid")Integer gcolorid,@PathVariable("count")Integer count,HttpServletRequest request){
        Integer userid = (Integer) request.getSession().getAttribute("userid");
        HashMap<String,String> map = new HashMap<String,String>();
        if(userid==null||userid==0){
            map.put("result","load");
        }
       try{
        Shoppingcar shoppingcar = new Shoppingcar();
        Goods goods = serviceH.getGoodsBygid(gid);
        shoppingcar.setGname(goods.getGname());
        shoppingcar.setPrice(goods.getPrice());
        shoppingcar.setImage(goods.getShowimages());
        Goodstype goodstype = serviceH.getTypeByid(gtyprid);
        shoppingcar.setGtypename(goodstype.getGtypename());
        shoppingcar.setDifprice(goodstype.getDifprice());
        Goodscolor goodscolor = serviceH.getColorByid(gcolorid);
        shoppingcar.setGcolorname(goodscolor.getGcolorname());
        shoppingcar.setGid(gid);
        shoppingcar.setGtypeid(gtyprid);
        shoppingcar.setGcolorid(gcolorid);
        shoppingcar.setCount(count);
        shoppingcar.setUserid(userid);
        int i = serviceH.AddMesToShopcar(shoppingcar);
        if(i>=1){
            map.put("result","true");
        }else{
            map.put("result","false");
        }
       }catch (Exception e){
           map.put("result","load");
       }
        return JSON.toJSONString(map);
    }
    /**
     * 跳转到购物车
     */
    @RequestMapping("/shoppingcart")
    public String toShopcar(HttpServletRequest request,Model model){
        Integer userid = (Integer) request.getSession().getAttribute("userid");
        if(userid==null||userid==0){
            return "before/login";
        }
        List allShoppingcar = serviceH.getAllShoppingcar(userid);
        model.addAttribute("shoppingcarli",allShoppingcar);
        return "before/shoppingcart";
    }
    /**
     * 购物车跳转到订单页
     */
    @RequestMapping("/order")
    public String ljxg(@RequestParam String cartItemIds,
                       @RequestParam Double total,@RequestParam Integer counts, Model model){
        //对数组进行操作
        String[] sids = cartItemIds.split(",");
        //将字符串转换为数组
        Integer[] sid = new Integer[sids.length];
        for(int i = 0;i < sids.length;i++){
            sid[i] = Integer.parseInt(sids[i]);
        }
    //    通过cartItemIds查询其型号、颜色、商品名称
        List<Shoppingcar> shoppingcars = serviceH.getShoppingcarByid(sid);
        model.addAttribute("shoppingcars",shoppingcars);
        /*随机数生成订单号*/
        int random=(int)(Math.random()*10000000+1);
        model.addAttribute("random",random);
        //总价
        model.addAttribute("total",total);
        //共几样商品
        model.addAttribute("counts",counts);
        return "before/ordercenterH";
    }
    @RequestMapping("/changecount/{sid}/{count}")
    @ResponseBody
    public String change(@PathVariable Integer sid,@PathVariable Integer count){
        int i = serviceH.changeCount(sid, count);
        if (i<1){
            return "false";
        }
        return "true";
    }
    @RequestMapping("/delcar/{sid}")
    @ResponseBody
    public String delcar(@PathVariable("sid") Integer sid){
        int i = serviceH.delcar(sid);
        if (i<1){
            return "false";
        }
        return "true";
    }

    @GetMapping("/contact")
    public String doContact(){
        return "before/contact";
    }
    //客服服务
    @PostMapping("/doContact")
    public String toContact(HttpServletRequest request, Feedback feedback){
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String type = request.getParameter("type");
        String suggest = request.getParameter("suggest");
        feedback.setUsername(name);
        feedback.setType(email);
        feedback.setPhone(phone);
        feedback.setType(type);
        feedback.setSuggest(suggest);
        boolean isAdd = serviceH.addSuggest(feedback);
        if(isAdd){
            return "redirect:/index";
        }else {
            return "before/contact";
        }
    }
    /**
     * 根据用户id获取地址
     */
    @RequestMapping("/loadAddress")
    @ResponseBody
    public List<Address> getAddress(HttpServletRequest request){
        Integer userid = (Integer) request.getSession().getAttribute("userid");
        List<Address> list = serviceH.getAddress(userid);
        return  list;
    }
}
